package com.ruoyi.project.system.task.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.task.domain.Task;
import com.ruoyi.project.system.task.service.TaskService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/system/task/result")
public class ResultController extends BaseController {


    private String prefix = "system/task/result";

    @Autowired
    private TaskService taskService;

    /**
     * 跳转到任务结果
     * @return
     */
    @RequiresPermissions("system:result:view")
    @GetMapping()
    public String task()
    {
        return prefix + "/result";
    }

    /**
     * 修改任务
     */
    @GetMapping("/edit/{taskId}")
    public String edit(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("task", taskService.selectTaskById(taskId));
        return prefix + "/edit";
    }

    /**
     * 修改保存公告
     */
    @RequiresPermissions("system:result:edit")
    @Log(title = "完成任务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Task task)
    {
        System.out.println(task.toString());
        System.out.println(task.getResult().toString());
        if (task.getResult().getResultId() == null){
            return toAjax(taskService.insertResult(task));
        } else {
            return toAjax(taskService.updateResult(task));
        }
    }

    /**
     * 更具条件分页查询已完成的任务
     * @param task 条件
     * @return
     */
    @RequiresPermissions("system:result:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectResultTaskList(task);
        return getDataTable(list);
    }

    /**
     * 一件导出表
     * @param task 查询条件
     * @return
     */
    @Log(title = "任务完成", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:result:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Task task)
    {
        List<Task> list = taskService.selectResultTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        return util.exportExcel(list, "任务完成数据");
    }

    /**
     * 根据任务的编号查看任务信息
     * @param taskId 任务编号
     * @param mmap
     * @return
     */
    @GetMapping("/show/{taskId}")
    public String show(@PathVariable("taskId") Long taskId, ModelMap mmap)
    {
        mmap.put("task", taskService.selectTaskById(taskId));
        return prefix + "/show";
    }

}
